package user;

import book.Book;
import book.Library;
import book.PairOfUserIdAndBookId;
import constant.Constant;
import utils.AnalyzingBook;
import utils.AnalyzingBorrowedBook;
import utils.PermissionException;
import utils.ScannerSingleton;

import java.io.IOException;
import java.util.Scanner;

public class NormalUser extends User{


    public Scanner scanner;

    public Library library;

    //存储图书借阅的信息
    public PairOfUserIdAndBookId[] pairOfUserIdAndBookId;

    //当前借阅书籍的数量
    public int borrowedCount;

    AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();

    public NormalUser(String name, int userID){
        super(name, userID);

        loadBorrowedBook();
        scanner = ScannerSingleton.getScannerSingleton();
        library = Library.getLibrary();
    }

    public void loadBorrowedBook() {
        PairOfUserIdAndBookId[] allBorrowedBook;

        try {
            //1.加载文件中的借阅信息
            allBorrowedBook = analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOKS_FILE_NAME);

            //2.默认借阅书籍数量为5
            pairOfUserIdAndBookId = new PairOfUserIdAndBookId[Constant.BORROW_BOOK_MAX_NUM];

            //3.没有读取到借阅信息
            if (allBorrowedBook == null) {
                borrowedCount = 0;
            }else {
                //4.如果读取的借阅书籍数量大于默认的书籍数量 进行扩容的操作
                if (allBorrowedBook.length > pairOfUserIdAndBookId.length) {
                    pairOfUserIdAndBookId = new PairOfUserIdAndBookId[allBorrowedBook.length];
                }

                //5.将文件读取的借阅书籍的信息 拷贝 到当前的借阅书籍中
                for (int i = 0; i < allBorrowedBook.length; i++) {
                    pairOfUserIdAndBookId[i] = allBorrowedBook[i];
                }

                //6.更新实际的借阅书籍的数量
                borrowedCount = allBorrowedBook.length;
            }

        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void storeBorrowed() {
        try {
            analyzingBorrowedBook.storeObject(pairOfUserIdAndBookId,Constant.BORROWED_BOOKS_FILE_NAME);

        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int display() {
        System.out.println(this.getName() + "的操作菜单：");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.借阅图书");
        System.out.println("5.归还图书");
        System.out.println("6.查看当前借阅情况");
//        return ScannerSingleton.getScanner();
        return scanner.nextInt();
    }




    //借阅图书
    public void borrowedBook() {

        scanner.nextLine();

        System.out.println("请输入你要借阅图书的书籍ID：");

        int ISBN = scanner.nextInt();
        scanner.nextLine();

        if(library.bookCount == 0) {
            System.out.println("图书馆没有书籍可以借阅....");
            return;
        }

        loadBorrowedBook();

        //1. 判断图书在图书馆当中 是否是存在的
        Book book = library.searchById(ISBN);

        if(book == null) {
            System.out.println("没有你借阅的图书，书籍ID为：" + ISBN);
            return;
        }

        //2.开始准备借阅图书
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUserIdAndBookId pairOfUidAndBookIds1 = pairOfUserIdAndBookId[i];

            if(pairOfUidAndBookIds1.getISBN() ==book.getISBN() ) {
                //getUserID当前用户的ID
                if(pairOfUidAndBookIds1.getUserId() == getUserID()) {
                    System.out.println("这本书是你自己已经借阅过的！");
                    return;
                }else {
                    System.out.println("这本书是别人已经借阅过的！");
                    return;
                }
            }
        }
        library.borrowBook(ISBN);
        //这里要写入  借阅书籍的txt


        PairOfUserIdAndBookId pairOfUidAndBookId = new PairOfUserIdAndBookId(getUserID(),book.getISBN());


        pairOfUserIdAndBookId[borrowedCount] = pairOfUidAndBookId;


        borrowedCount++;

        storeBorrowed();

        System.out.println("借阅书籍成功！！！");

    }

    //归还图书
    public void returnBook() {
        loadBorrowedBook();

        if (borrowedCount == 0) {
            System.out.println("目前没有用户借阅过书籍");
            return;
        }

        scanner.nextLine();
        System.out.println("请输入你要归还图书的id：");
        int ISBN = scanner.nextInt();
        scanner.nextLine();

        //判断要借阅的书 是否是已经被自己借阅过了
        Book book = library.searchById(ISBN);
        if(book == null) {
            System.out.println("没有该id的相关书籍："+ISBN);
            return;
        }

        for (int i = 0; i < borrowedCount; i++) {
            //如果2本书的ID，则认为是同一本书
            if (pairOfUserIdAndBookId[i].getISBN()==book.getISBN()) {
                //借阅用户也一样
                if (getUserID() == pairOfUserIdAndBookId[i].getUserId()) {
                    library.returnBook(ISBN);
                    System.out.println("图书 '" + book.getTitle() + "' 已成功归还。");
                    // 用最后一本替换归还的书
                    pairOfUserIdAndBookId[i] = pairOfUserIdAndBookId[borrowedCount - 1];
                    // 清空最后一个
                    pairOfUserIdAndBookId[borrowedCount - 1] = null;
                    borrowedCount--;
                    storeBorrowed();
                }else{
                    System.out.println("该书籍不是你借阅的书籍，不能归还："+book.getTitle());
                }
                return;
            }
        }
        System.out.println("你没有借阅该书籍，不需要归还，书籍 ID 为： "+ISBN);

    }

    //查看当前借阅情况
    public void viewBorrowedHistory() {
        //读取当前借阅所有用户的借阅书籍的情况
        loadBorrowedBook();

        System.out.println("您的借阅情况如下：");

        if (borrowedCount == 0) {
            System.out.println("目前没有借阅记录.....");
        } else {
            boolean flg = false;

            for (int i = 0; i < borrowedCount; i++) {
                //这里只能查看属于自己借阅的情况
                //用户ID相同的情况下，使用书籍ID查询书籍
                if(pairOfUserIdAndBookId[i].getUserId() == getUserID()) {
                    flg = true;
                    Book book = library.searchById(pairOfUserIdAndBookId[i].getISBN());
                    System.out.println(book);
                }
            }
            if(!flg) {
                System.out.println("你没有借阅过书籍！");
            }
        }
    }

}





